package com.zhuliwei.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zhuliwei.constants.SystemConstants;
import com.zhuliwei.domain.entity.Comment;
import com.zhuliwei.mapper.ArticleMapper;
import com.zhuliwei.mapper.CommentMapper;
import com.zhuliwei.mapper.UserMapper;
import com.zhuliwei.service.HomeService;
import com.zhuliwei.utils.RedisCache;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 朱力维
 * @time 2023/7/22 10:41
 **/
@Service
public class HomeServiceImpl implements HomeService {
    @Resource
    private UserMapper userMapper;
    @Resource
    private CommentMapper commentMapper;
    @Resource
    private ArticleMapper articleMapper;
    @Resource
    private RedisCache redisCache;
    @Override
    public Map<String,Integer> lineCount() {
        // 文章总浏览量
        Map<String,Integer> viewCountMap = redisCache.getCacheMap(SystemConstants.VIEWCOUNT_MAP_PREFIX);
        int viewsCount = 0;
        for (Integer value : viewCountMap.values()) {
            viewsCount+=value;
        }
        Map<String,Integer> map = new HashMap<>();
        map.put("article", articleMapper.selectList(null).size());
        // 留言数
        LambdaQueryWrapper<Comment> lqw = new LambdaQueryWrapper();
        lqw.eq(Comment::getType,SystemConstants.LINK_COMMENT);
        map.put("message",commentMapper.selectList(lqw).size());
        map.put("user",userMapper.selectCount(null).intValue());
        map.put("viewsCount",viewsCount);
        return map;
    }
}
